package com.tsmti.system.service.impl;


import com.tsmti.core.common.service.impl.BaseServiceImpl;
import com.tsmti.core.common.constant.RedisConstants;
import com.tsmti.core.util.ObjectUtils;
import com.tsmti.core.util.RedisUtils;
import com.tsmti.system.entity.Organization;
import com.tsmti.system.service.OrganizationService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * Title：PimsSystem
 * Class：com.ts.sys.service.impl
 * Comapny:
 *
 * @author：zcw
 * @version：2.5 CreateTime：2018/1/17 17:06
 * Modify log:
 * Description：
 */

@Transactional(rollbackFor = Exception.class)
@Service("organizationService")
public class OrganizationServiceImpl extends BaseServiceImpl<Organization> implements OrganizationService {
    @Override
    public List<Organization> findListByUserId(long userid) {
         return findByHql("from Organization where  id in (SELECT uo.organizationId from UserOrganization uo WHERE uo.userId = ?)", userid);
    }

    @Override
    public Organization save(Organization entity) {
        if (!ObjectUtils.isNullOrEmpty(entity.getParentId())) {
            Organization parent = get(entity.getParentId());
            entity.setParentId(parent.getId());
            entity.setParentIds(parent.makeSelfAsNewParentIds());
        } else {
            entity.setParentId(null);
        }
        try {
            updateCache(RedisConstants.ORGANIZATION_CACHE,entity.getCode(),entity);
        }catch (Exception e){
            logger.warn("更新机构缓存失败!",e);
        }
        entity = super.save(entity);
        return entity;
    }

    /**
     * 批量保存
     * @param entitys
     */
    @Override
    public void saveAll(List<Organization> entitys){
        Map<String,Organization> allMap = null;
        try {
            allMap =  (Map<String, Organization>) RedisUtils.getObject(RedisConstants.ORGANIZATION_CACHE);
            logger.info("读取机构字典缓存成功!");
        } catch (Exception e) {
            logger.warn("读取机构字典缓存失败!");
            e.printStackTrace();
        }
        super.saveAll(entitys);
        for (Organization organization:entitys){
            allMap.put(organization.getCode(),organization);
        }
        try {
            super.updateBathCache(RedisConstants.ORGANIZATION_CACHE,allMap);
        } catch (Exception e) {
            logger.warn("更新机构字典缓存失败!");
            e.printStackTrace();
        }
    }

    @Override
    public void update(Organization entity){
        if (!ObjectUtils.isNullOrEmpty(entity.getParentId())) {
            Organization parent = get(entity.getParentId());
            entity.setParentId(parent.getId());
            entity.setParentIds(parent.makeSelfAsNewParentIds());
        } else {
            entity.setParentId(null);
        }
        super.update(entity);
        try {
            updateCache(RedisConstants.ORGANIZATION_CACHE,entity.getCode(),entity);
        }catch (Exception e){
            logger.warn("更新机构缓存失败!",e);
        }

    }
}
